package com.example.tbag.healthcare.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.tbag.healthcare.Activity.BedActivity;
import com.example.tbag.healthcare.Activity.DragActivity;
import com.example.tbag.healthcare.Activity.HealthActivity;
import com.example.tbag.healthcare.Activity.HumanActivity;
import com.example.tbag.healthcare.Model.Person;
import com.example.tbag.healthcare.R;

import java.util.List;

/**
 * Created by tbag on 2017/4/21.
 */

public class MainListAdapter extends BaseAdapter {
    private List<Person> persons;
    private LayoutInflater mInflater;
    private Context context;
    private Intent intent;
    
    public MainListAdapter(List<Person> list,Context context,int type){
        persons = list;
        mInflater = LayoutInflater.from(context);
        this.context = context;
        switch (type){
            case 0:
                intent = new Intent(context, BedActivity.class);
                break;
            case 1:
                intent = new Intent(context, HumanActivity.class);
                break;
            case 2:
                intent = new Intent(context, HealthActivity.class);
                break;
            case 3:
                intent = new Intent(context, DragActivity.class);
                break;
        }
    }
    
    
    @Override
    public int getCount() {
        return persons.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_main_list, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.edit1);
            holder.sex = (TextView) convertView.findViewById(R.id.edit2);
            holder.age = (TextView)convertView.findViewById(R.id.edit3);
            holder.type= (TextView)convertView.findViewById(R.id.edit4);
            holder.bed =(TextView)convertView.findViewById(R.id.edit5);
            convertView.setTag(holder); //绑定ViewHolder对象
        }
        else {
            holder = (ViewHolder) convertView.getTag(); //取出ViewHolder对象
        }
        final Person person = persons.get(position);
        holder.name.setText(person.name);
        holder.sex.setText(person.sex);
        holder.age.setText(person.age);
        holder.bed.setText(person.bedNum);
        holder.type.setText(person.type);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("id",person.id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public final class ViewHolder {
        public TextView name;
        public TextView sex;
        public TextView age;
        public TextView bed;
        public TextView type;
    }
}
